﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://wpcontrol.sourceforge.net/ for more.
//
// Copyright (c) 2014 nscrolldev
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace WallpaperControl
{
    /// <summary>
    /// Manages the applications start mode.
    /// </summary>
    class StartManager
    {
        /// <summary>
        /// Stores the start mode setting in the registry, so that the app is 
        /// executed on startup if requested by user.
        /// </summary>
        internal void StoreStartMode()
        {
            // Open the registry key responsible for windows autostart
            RegistryKey startKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            // App should start automatically
            if (Program.Configuration.StartWithWindows)
            {
                // Write key
                startKey.SetValue("WallpaperControl", Application.ExecutablePath.ToString());
            }
            // App should not start automatically
            else
            {
                // Delete key
                startKey.DeleteValue("WallpaperControl", false);
            }
        }

        /// <summary>
        /// Gets the applications start mode.
        /// </summary>
        /// <returns>True if application currently start automatically, false otherwise.</returns>
        internal bool GetStartMode()
        {
            bool autostart;
            RegistryKey startKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", false);
            if (startKey.GetValue("WallpaperControl") == null)
            {
                autostart = false;
            }
            else
            {
                autostart = true;
            }
            return autostart;
        }
    }
}
